package com.chaoxing.service.impl;

import com.chaoxing.entity.User;
import com.chaoxing.mapper.UserMapper;
import com.chaoxing.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public String selectByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }

    @Override
    public Integer loginSys(String phone, String pwd) {
        return userMapper.loginSys(phone, pwd);
    }

    @Override
    public User selectUser(String phone, String pwd) {
        return userMapper.selectUser(phone, pwd);
    }

    @Override
    public List<User> selectAllUserByAd(int pageNo, int pageSize) {
        return userMapper.selectAllUserByAd(pageNo, pageSize);
    }

    @Override
    public int deleteUserByAd(int userid) {
        return userMapper.deleteUserByAd(userid);
    }

    @Override
    public List<Integer> selectUserId(int id) {
        return userMapper.selectUserId(id);
    }

    @Override
    public int deleteUserById(int id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public int updateUserByAd(Integer id, String pwd) {
        return userMapper.updateUserByAd(id, pwd);
    }

    @Override
    public String selectId(Integer id) {
        return userMapper.selectId(id);
    }

    //查询所有学生数量
    @Override
    public int queryAllStNum() {
        return userMapper.queryAllStNum();
    }

    //查询已经选了课的学生数量
    @Override
    public int queryPartStNum(int id) {
        return userMapper.queryPartStNum(id);
    }

    @Override
    public Map<String, String> calculation(int id) {
        Map<String, String> map = new HashMap<>();
        //查询所有学生数量
        int allStNum = queryAllStNum();
        //查询选id课学生数量
        int partStNum = queryPartStNum(id);
        //返回id这门课  选择/全部  比例
        double selected = (double) partStNum / allStNum;
        Double unselected = 1 - selected;
        map.put("selected", String.format("%.2f", selected));
        map.put("unselected", String.format("%.2f", unselected));
        return map;
    }

    @Override
    public Integer exitSystem(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        session.removeAttribute("User");
        return 1;
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> selectTeacher() {
        return userMapper.selectTeacher();
    }

    @Override
    public User queryUser(String userid) {
        return userMapper.queryUser(userid);
    }
}
